/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月7日         cassie
 */

package com.coocaa.launcher.pattern.normal.allapp;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import com.coocaa.launcher.framework.launcherhost.view.AppItemView;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppItemData;
import com.coocaa.launcher.pattern.normal.UIConfig;
import com.coocaa.launcher.pattern.normal.hotseat.FocusView;
import com.coocaa.launcher.pattern.normal.util.ItemViewFactory;
import com.coocaa.launcher.util.Utils;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

public class AllAppMenuView extends BaseView {
    public static final int ADD_TO_HOT_SEAT_BTN_ID = 1;
    public static final int REMOVE_APP_BTN_ID = 2;

    private IconButton addToHotSeatBtn;
    private IconButton removeBtn;
    private LayoutParams menuItemParams;
    private int xPadding;
    public static int menuHeight = SkyConstants.Div(258);
    int borderWidth = UIConfig.focusBorderWidth;
    private int iconBtnHeight = menuHeight - 2 * borderWidth;
    private int itemSpacing = SkyConstants.Div(71);
    private int addBtnLeft;
    private int removeBtnLeft;
    private int adjustBorder;
    private String itemTag = "app_item";

    private FocusView focusView;

    public AllAppMenuView(Context context, int xPadding) {
        super(context);
        this.xPadding = xPadding;
        adjustBorder = SkyConstants.Div(12);
        // TODO Auto-generated constructor stub
        createView();
    }

    private void createView() {
        SkinManager.getInstance().setBackgroundResource(this, R.drawable.all_app_menu_bg);
        addToHotSeatBtn = new IconButton(mContext);
        addToHotSeatBtn.setIcon(R.drawable.icon_add_to_hot_seat);
        addToHotSeatBtn.setText(R.string.add_to_hot_seat);
        addToHotSeatBtn.setId(ADD_TO_HOT_SEAT_BTN_ID);
        addToHotSeatBtn.setOnFocusChangeListener(focusListener);

        removeBtn = new IconButton(mContext);
        removeBtn.setIcon(R.drawable.icon_uninstall);
        removeBtn.setText(R.string.uninstall_app);
        removeBtn.setId(REMOVE_APP_BTN_ID);
        removeBtn.setOnFocusChangeListener(focusListener);

        LayoutParams addParams = new LayoutParams(UIConfig.hotSeatItemWidth,
                iconBtnHeight, Gravity.CENTER_VERTICAL);
        addBtnLeft = Utils.getScreenWidth(mContext) - xPadding - itemSpacing -
                UIConfig.hotSeatItemWidth * 2 - adjustBorder * 2;
        addParams.leftMargin = addBtnLeft;
        this.addView(addToHotSeatBtn, addParams);
        LayoutParams removeParams = new LayoutParams(UIConfig.hotSeatItemWidth,
                iconBtnHeight, Gravity.CENTER_VERTICAL);
        removeBtnLeft = Utils.getScreenWidth(mContext) - xPadding -
                adjustBorder - UIConfig.hotSeatItemWidth;
        removeParams.leftMargin = removeBtnLeft;
        this.addView(removeBtn, removeParams);

        menuItemParams = new LayoutParams(UIConfig.hotSeatItemWidth,
                LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.CENTER_VERTICAL);
        menuItemParams.leftMargin = xPadding + adjustBorder;

        focusView = new FocusView(mContext);
        SkinManager.getInstance().setBackgroundResource(focusView, R.drawable.hot_seat_focus_bg);

        this.addView(focusView, new LayoutParams(UIConfig.hotSeatItemWidth + 2 * borderWidth,
                iconBtnHeight + 2 * borderWidth));
    }

    public void addOrReplaceAppItemView(AppItemData data) {
        AppItemView view = ItemViewFactory.createItemView(mContext, data);
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);
        View oldItem = this.findViewWithTag(itemTag);
        if (oldItem != null)
            this.removeView(oldItem);
        view.setTag(itemTag);
        this.addView(view, 0, menuItemParams);
        LogUtils.d("app", "addOrReplaceAppItemView   pkg:" + data.getPackageName() + " issystem:" + data.isSystem());
        if (data.isSystem()) {
            removeBtn.setEnabled(false);
            removeBtn.setAlpha(0.3f);
            removeBtn.setFocusable(false);
        } else {
            removeBtn.setEnabled(true);
            removeBtn.setAlpha(1);
            removeBtn.setFocusable(true);
        }
    }

    public void setBtnClickListener(OnClickListener listener) {
        addToHotSeatBtn.setOnClickListener(listener);
        removeBtn.setOnClickListener(listener);
    }

    public void setAddBtnEnabled(boolean enabled) {
        addToHotSeatBtn.setEnabled(enabled);
    }

    public void resetFocus() {
        LayoutParams focusParams = (LayoutParams) focusView.getLayoutParams();
        if (addToHotSeatBtn.isEnabled()) {
            focusParams.leftMargin = addBtnLeft - borderWidth;
            addToHotSeatBtn.requestFocus();
        } else {
            focusParams.leftMargin = removeBtnLeft - borderWidth;
            removeBtn.requestFocus();
        }
        focusView.setLayoutParams(focusParams);
    }

    @Override
    public void onThemeChange() {
        // TODO Auto-generated method stub
        super.onThemeChange();
        SkinManager.getInstance().setBackgroundResource(this, R.drawable.all_app_menu_bg);
        SkinManager.getInstance().setBackgroundResource(focusView, R.drawable.hot_seat_focus_bg);
    }

    private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (hasFocus) {
                if (v.getWidth() > 0 && v.getHeight() > 0) {
                    focusView.changeFocusPos(v.getLeft() - borderWidth,
                            v.getTop() - borderWidth, v.getWidth() + borderWidth * 2,
                            v.getHeight() + borderWidth * 2);
                }
            } else
                v.setBackgroundColor(Color.TRANSPARENT);
        }
    };
}
